package bb.lanxing.lib.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Region;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import androidx.appcompat.widget.AppCompatTextView;

public class VerticalTextView extends AppCompatTextView {
    private final boolean topDown;

    public VerticalTextView(Context context) {
        this(context, null, 0);
    }

    public VerticalTextView(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public VerticalTextView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        int gravity = getGravity();
        if (Gravity.isVertical(gravity) && (gravity & Gravity.FILL_VERTICAL) == Gravity.BOTTOM) {
            setGravity((gravity & Gravity.FILL_HORIZONTAL) | Gravity.TOP);
            this.topDown = false;
            return;
        }
        this.topDown = true;
    }

    @Override
    protected void onMeasure(int i, int i2) {
        super.onMeasure(i2, i);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    @Override
    protected boolean setFrame(int i, int i2, int i3, int i4) {
        return super.setFrame(i, i2, (i4 - i2) + i, (i3 - i) + i2);
    }

    @Override
    public void draw(Canvas canvas) {
        canvas.translate(0.0f, getWidth());
        canvas.rotate(-90.0f);
        if (Build.VERSION.SDK_INT >= 26) {
            canvas.clipRect(0, 0, getWidth(), getHeight());
        } else {
            canvas.clipRect(0.0f, 0.0f, getWidth(), getHeight(), Region.Op.REPLACE);
        }
        super.draw(canvas);
    }
}
